<?php

class sbBankIsrael {
    public function downloadXml() {
         return file_get_contents('http://www.bankisrael.gov.il/currency.xml', true);
    }
    
    public function parseXml($file) {
        $xml = simplexml_load_string($file);
        
        $dollar = array('Date' => $xml->LAST_UPDATE->__toString());
        
        foreach ($xml->CURRENCY as $v) {
            if ($v->CURRENCYCODE == 'USD') {
                $dollar['Code'] = $v->CURRENCYCODE->__toString();
                $dollar['Rate'] = $v->RATE->__toString();
            }
        }
        
        return $dollar;
    }
    
    public function saveResult($result) {
        $bank = tblBankTable::getInstance()->findByDql('date=?', $result['Date'])->getFirst();
        
        if ($bank) {
            $bank->setRate($result['Rate']);
            $bank->setType($result['Code']);
            $bank->setCorrect(sbValues::DOLLAR_CORRECT);
            $bank->setNds(sbValues::DOLLAR_NDS);
            $bank->setCurrent(($result['Rate'] * sbValues::DOLLAR_CORRECT) + $result['Rate']);
            $bank->save();
        } else {
            $bank = new tblBank();
            $bank->setDate($result['Date']);
            $bank->setRate($result['Rate']);
            $bank->setType($result['Code']);
            $bank->setCorrect(sbValues::DOLLAR_CORRECT);
            $bank->setNds(sbValues::DOLLAR_NDS);
            $bank->setCurrent(($result['Rate'] * sbValues::DOLLAR_CORRECT) + $result['Rate']);
            $bank->save();
        }
    }
}

